package handler

import (
	"car-rental-08-14/pkg"
	"github.com/gin-gonic/gin"
	"net/http"
	"strings"
)

func Upload(c *gin.Context) {

	file, err := c.FormFile("image")
	if err != nil {
		return
	}

	ect := strings.ToLower(file.Filename)
	if !strings.Contains(".png.jpg", ect) {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "上传的图片格式仅为 png、jpg",
			"data":    "",
		})
		return
	}

	if file.Size > 2*1024*1024 {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "文件大小不超过200kb",
			"data":    "",
		})
		return
	}

	upload := pkg.Upload(file)
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "上传成功",
		"data":    upload,
	})
}
